package com.ysxsoft.common_base.view.custom;

import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.view.ViewCompat;

import com.ysxsoft.common_base.R;
import com.ysxsoft.common_base.utils.DisplayUtils;

public class CirclePercentView extends View {
    private Paint paint;
    private Paint textPaint;
    private Context mContext;
    private int start=0;//当前进度
    private int percent=90;//当前百分比
    //扩展属性
    private int normalColor;//圆底色
    private int selectColor;//彩色圆
    private int radius;//圆半径
    private float circleWidth;//圆边宽度
    private int width;
    private int height;
    private float centerTextSize;
    private int centerTextColor;

    public CirclePercentView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CirclePercentView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attributeSet) {
        this.mContext = context;
        TypedArray array = mContext.obtainStyledAttributes(attributeSet,R.styleable.CirclePercentView);
        normalColor = array.getColor(R.styleable.CirclePercentView_normalCircleColor, mContext.getResources().getColor(R.color.color_spacing));
        selectColor = array.getColor(R.styleable.CirclePercentView_selectCircleColor, mContext.getResources().getColor(R.color.colorPrimary));
        radius = array.getInteger(R.styleable.CirclePercentView_percentRadius, DisplayUtils.dp2px(mContext, 2));
        circleWidth = array.getDimension(R.styleable.CirclePercentView_circleWidth, DisplayUtils.dp2px(mContext, 2));
        centerTextSize = array.getDimension(R.styleable.CirclePercentView_strTextSize, DisplayUtils.sp2px(mContext, 16));
        centerTextColor = array.getColor(R.styleable.CirclePercentView_strTextColor, mContext.getResources().getColor(R.color.color_spacing));
        initPaint();
    }

    private void initPaint() {
        paint = new Paint();
        paint.setColor(normalColor);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(circleWidth);
        paint.setStrokeCap(Paint.Cap.ROUND);
        paint.setDither(true);
        paint.setAntiAlias(true);
        textPaint=new Paint();
        textPaint.setTextSize(centerTextSize);
        textPaint.setColor(centerTextColor);
        textPaint.setStrokeCap(Paint.Cap.ROUND);
        paint.setDither(true);
        paint.setAntiAlias(true);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        width = getWidth();
        height = getHeight();
        radius = width / 2- (int)circleWidth / 2;
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        Log.e("tag", "onAttachedToWindow");
        startAnim();
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        Log.e("tag", "onDetachedFromWindow");
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Log.e("tag","onDraw");
        paint.setColor(normalColor);
        canvas.drawCircle(width / 2, height / 2, radius, paint);
        RectF rect = new RectF(circleWidth / 2, circleWidth / 2, width - circleWidth / 2, height - circleWidth / 2);
        paint.setColor(selectColor);
        canvas.drawArc(rect, -90, getRealPercent(), false, paint);

        String str=start+"%";
        Rect rect1=new Rect();
        textPaint.getTextBounds(str,0,str.length(),rect1);
        float w=rect1.width();
        float h=rect1.height();
        canvas.drawText(str,width/2-w/2,radius+h/2,textPaint);
    }

    public void startAnim(){
        ValueAnimator valueAnimator=ValueAnimator.ofInt(percent);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                start= (int) animation.getAnimatedValue();
                postInvalidate();
            }
        });
        valueAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
        valueAnimator.setDuration(700);
        valueAnimator.setRepeatCount(0);
        valueAnimator.start();
    }

    private float getRealPercent() {
        float result = start*360/100;
        return result;
    }

    public void setSelectColor(int selectColor) {
        this.selectColor = selectColor;
        start=0;
        startAnim();
    }

    public void setPercentAndColor(int percent,int selectColor) {
        this.percent = percent;
        this.selectColor = selectColor;
        start=0;
        startAnim();
    }

}
